package com.seaengine.actor.anibmp
{
	import com.seaengine.GameConfig;
	import com.seaengine.resourcemanager.ResSystem;
	
	public class BaseBmpMgr
	{
		public var m_name:String;
		public var m_source:String;
		public var m_Id:int;
		
		private var m_xml:XML;
		private var m_successFn:Function;
		
		protected var m_bitmapRes:Object;
		protected var m_animationBmps:Object;
		protected var m_FrameInfos:Object;
		public var m_bLoaded:Boolean = false;

		public function BaseBmpMgr(_Id:int,_xml:XML,_SuccessFn:Function)
		{
			m_Id = _Id;
			m_bitmapRes = {};
			m_animationBmps = {};
			m_FrameInfos = {};
			m_successFn = _SuccessFn;
			loadXML(_xml);		
		}
		
		public function loadXML(_xml:XML):void
		{
			m_name = _xml.@name;
			m_source = _xml.@source;
			specialInfo(_xml);
			m_xml = _xml;
			
			ResSystem.getInstance().getRes(GameConfig.m_ResConfig + m_source,onFinishLoaded);
		}
		
		public function onFinishLoaded(_fileName:String,obj:Object):void
		{
			var _swfobj:Object = obj;
			genBitmapResList(_swfobj,_fileName);
			
			genAnimationBitmap();
			m_xml = null;
			m_bLoaded = true;
			m_successFn(this);			
			m_successFn = null;	
				
		}
		private function genBitmapResList(_swfobj:Object,_fileName:String):void
		{
			for each( var el:XML in m_xml..frame)
			{
				if (m_bitmapRes[el.@Source]|| el.@Source == '')
				{
					
					continue;
				}
				
				m_bitmapRes[el.@Source] = ResSystem.getInstance().getAppDomainBitmapData(_fileName,el.@Source);			
			}		
		}
		
		private function genAnimationBitmap():void
		{
			for each( var el:XML in m_xml.*)
			{				
				var _ar:Array = new Array();
				
				for each( var en:XML in el.*)
				{								
					_ar.push( m_bitmapRes[en.@Source]);
				}
				m_animationBmps[el.@id] = _ar;
				genAnimationInfo(el);											
			}	
		}
		protected function genAnimationInfo(_xml:XML):void
		{
		}
		
		protected function specialInfo(_xml:XML):void
		{
		
		}	
		public function getNodeInfo(_animationId:int):BaseBmpNodeInfo
		{
			if(m_animationBmps[_animationId] == null)
				return null;
			return new BaseBmpNodeInfo(_animationId,m_animationBmps[_animationId],m_FrameInfos[_animationId]);
		}	
	}
}